package cn.goktech.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import com.alibaba.fastjson.JSON;

import cn.goktech.entity.Share;
import cn.goktech.service.IShareService;
import cn.goktech.service.impl.ShareServiceImpl;

@WebServlet("/ShareServlet")
public class ShareServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String task=request.getParameter("task");
		
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		
		IShareService shareService = new ShareServiceImpl();
		if("queryShareList".equals(task)){
			String username = request.getParameter("username");
			List<Share> shareList = shareService.sharList(username);
			request.setAttribute("shareList", shareList);
			request.getRequestDispatcher("user/share.jsp").forward(request, response);
		}else if("addShare".equals(task)){
			String username = (String)session.getAttribute("username");
			try {
				Map<String, String> map = parseRequest(request);
				Share share = new Share();
				share.setContent(map.get("content"));
				share.setTitle(map.get("title"));
				share.setImg(map.get("img"));
				share.setUsername(username);
				int add = shareService.add(share);
				if(add>0){
					out.println("<script>window.location.href='ShareServlet?task=queryShareList&username="+username+"'</script>");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("deleteShare".equals(task)){
			int id=Integer.parseInt(request.getParameter("id"));
			System.out.println(id);
			int delete = shareService.delete(id);
			if(delete>0){
				out.write("true");
			}
		}else if("loadmore".equals(task)){
			String username = request.getParameter("username");
			List<Share> shareList = shareService.sharList(username);
			String jsonString = JSON.toJSONString(shareList);
			out.print(jsonString);
		}
	}
	
	
private Map<String, String> parseRequest(HttpServletRequest request) throws Exception {
		
		Map<String, String> map = new HashMap<String,String>();
		
		DiskFileUpload fileUpload = new DiskFileUpload();
		
		List<FileItem> list = fileUpload.parseRequest(request);
		for (FileItem fileItem : list) {
			if(fileItem.isFormField()){
				String name=fileItem.getFieldName();
				String value = fileItem.getString("utf-8");
				map.put(name, value);
			}else{
				String name=fileItem.getFieldName();
				String oldName = fileItem.getName();
				String newName = makeNewName(oldName);
				String realPath = "F:/images/"+newName;
				fileItem.write(new File(realPath));
				map.put(name, newName);
			}
		}
		
		return map;
	}

	private String makeNewName(String oldName) {
		String newName = null;
		String type = oldName.substring(oldName.lastIndexOf("."));
		
		newName = UUID.randomUUID().toString();
		
		return newName+type;
	}


	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
